﻿#region Copyright (c) 2013-06, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

#if TESTS

using System;
using Amarok.Agents;
using NUnit.Framework;


namespace Amarok.Presentation
{
	/// <summary>
	/// </summary>
	[TestFixture]
	public class Test_WindowDescription
	{
		public class UiTestWindow : ViewModel
		{
		}

		public class TestWindow : ViewWindow
		{
		}

		public class TestWindowController : Controller<UiTestWindow>
		{
			public TestWindowController(AgentEnvironment environment, AgentOptions options, IView view, UiTestWindow viewModel)
				: base(environment, options, view, viewModel)
			{
			}
		}

		private class TestWindowDescription : WindowDescription<
			TestWindow,
			TestWindowController,
			UiTestWindow>
		{
		}


		// --- TESTS ---


		[Test]
		public void Test_Instantiate_Dispose()
		{
			var desc = new TestWindowDescription();

			Assert.That(desc.ViewType, Is.EqualTo(typeof(TestWindow)));
			Assert.That(desc.ControllerType, Is.EqualTo(typeof(TestWindowController)));

			Assert.That(desc.Completion, Is.Not.Null);
			Assert.That(desc.Completion.IsCompleted, Is.False);
			Assert.That(desc.Completion.IsCanceled, Is.False);
			Assert.That(desc.Completion.IsFaulted, Is.False);

			Assert.That(desc.IsCreated, Is.False);
			Assert.That(desc.Controller, Is.Null);
			Assert.That(desc.View, Is.Null);
			Assert.That(desc.Window, Is.Null);

			desc.Dispose();

			Assert.That(desc.ViewType, Is.EqualTo(typeof(TestWindow)));
			Assert.That(desc.ControllerType, Is.EqualTo(typeof(TestWindowController)));

			Assert.That(desc.Completion, Is.Not.Null);
			Assert.That(desc.Completion.IsCompleted, Is.True);
			Assert.That(desc.Completion.IsCanceled, Is.False);
			Assert.That(desc.Completion.IsFaulted, Is.False);

			Assert.That(desc.IsCreated, Is.False);
			Assert.That(desc.Controller, Is.Null);
			Assert.That(desc.View, Is.Null);
			Assert.That(desc.Window, Is.Null);
		}

		[Test]
		public void Test_Instantiate_Complete()
		{
			var desc = new TestWindowDescription();

			Assert.That(desc.ViewType, Is.EqualTo(typeof(TestWindow)));
			Assert.That(desc.ControllerType, Is.EqualTo(typeof(TestWindowController)));

			Assert.That(desc.Completion, Is.Not.Null);
			Assert.That(desc.Completion.IsCompleted, Is.False);
			Assert.That(desc.Completion.IsCanceled, Is.False);
			Assert.That(desc.Completion.IsFaulted, Is.False);

			Assert.That(desc.IsCreated, Is.False);
			Assert.That(desc.Controller, Is.Null);
			Assert.That(desc.View, Is.Null);
			Assert.That(desc.Window, Is.Null);

			desc.Complete(false);
			desc.Completion.Wait();

			Assert.That(desc.ViewType, Is.EqualTo(typeof(TestWindow)));
			Assert.That(desc.ControllerType, Is.EqualTo(typeof(TestWindowController)));

			Assert.That(desc.Completion, Is.Not.Null);
			Assert.That(desc.Completion.IsCompleted, Is.True);
			Assert.That(desc.Completion.IsCanceled, Is.False);
			Assert.That(desc.Completion.IsFaulted, Is.False);

			Assert.That(desc.IsCreated, Is.False);
			Assert.That(desc.Controller, Is.Null);
			Assert.That(desc.View, Is.Null);
			Assert.That(desc.Window, Is.Null);
		}

		[Test, RequiresSTA]
		public void Test_Create_Dispose()
		{
			var environment = new AgentEnvironment();
			var options = new AgentOptions();
			var desc = new TestWindowDescription();

			Assert.That(desc.ViewType, Is.EqualTo(typeof(TestWindow)));
			Assert.That(desc.ControllerType, Is.EqualTo(typeof(TestWindowController)));

			Assert.That(desc.Completion, Is.Not.Null);
			Assert.That(desc.Completion.IsCompleted, Is.False);
			Assert.That(desc.Completion.IsCanceled, Is.False);
			Assert.That(desc.Completion.IsFaulted, Is.False);

			Assert.That(desc.IsCreated, Is.False);
			Assert.That(desc.Controller, Is.Null);
			Assert.That(desc.View, Is.Null);
			Assert.That(desc.Window, Is.Null);

			desc.Create(environment, options);

			Assert.That(desc.ViewType, Is.EqualTo(typeof(TestWindow)));
			Assert.That(desc.ControllerType, Is.EqualTo(typeof(TestWindowController)));

			Assert.That(desc.Completion, Is.Not.Null);
			Assert.That(desc.Completion.IsCompleted, Is.False);
			Assert.That(desc.Completion.IsCanceled, Is.False);
			Assert.That(desc.Completion.IsFaulted, Is.False);

			Assert.That(desc.IsCreated, Is.True);
			Assert.That(desc.Controller, Is.Not.Null);
			Assert.That(desc.Controller, Is.TypeOf<TestWindowController>());
			Assert.That(desc.Completion.IsCompleted, Is.False);
			Assert.That(desc.View, Is.Not.Null);
			Assert.That(desc.View, Is.TypeOf<TestWindow>());
			Assert.That(desc.Window, Is.Not.Null);
			Assert.That(desc.Window, Is.SameAs(desc.View));
			Assert.That(View.GetEnvironment(desc.Window), Is.SameAs(environment));
			Assert.That(View.GetOptions(desc.Window), Is.SameAs(options));

			desc.Dispose();

			Assert.That(desc.ViewType, Is.EqualTo(typeof(TestWindow)));
			Assert.That(desc.ControllerType, Is.EqualTo(typeof(TestWindowController)));

			Assert.That(desc.Completion, Is.Not.Null);
			Assert.That(desc.Completion.IsCompleted, Is.True);
			Assert.That(desc.Completion.IsCanceled, Is.False);
			Assert.That(desc.Completion.IsFaulted, Is.False);

			Assert.That(desc.IsCreated, Is.True);
			Assert.That(desc.Controller, Is.Not.Null);
			Assert.That(desc.Controller, Is.TypeOf<TestWindowController>());
			Assert.That(desc.Completion.IsCompleted, Is.True);
			Assert.That(desc.View, Is.Not.Null);
			Assert.That(desc.View, Is.TypeOf<TestWindow>());
			Assert.That(desc.Window, Is.Not.Null);
			Assert.That(desc.Window, Is.SameAs(desc.View));
			Assert.That(View.GetEnvironment(desc.Window), Is.SameAs(environment));
			Assert.That(View.GetOptions(desc.Window), Is.SameAs(options));
		}

		[Test, RequiresSTA]
		public void Test_Create_With_ViewModel()
		{
			var environment = new AgentEnvironment();
			var options = new AgentOptions();
			var desc = new TestWindowDescription();
			var viewModel = new UiTestWindow();

			Assert.That(desc.ViewType, Is.EqualTo(typeof(TestWindow)));
			Assert.That(desc.ControllerType, Is.EqualTo(typeof(TestWindowController)));

			Assert.That(desc.Completion, Is.Not.Null);
			Assert.That(desc.Completion.IsCompleted, Is.False);
			Assert.That(desc.Completion.IsCanceled, Is.False);
			Assert.That(desc.Completion.IsFaulted, Is.False);

			Assert.That(desc.IsCreated, Is.False);
			Assert.That(desc.Controller, Is.Null);
			Assert.That(desc.View, Is.Null);
			Assert.That(desc.Window, Is.Null);

			desc.Create(environment, options, viewModel);

			Assert.That(desc.ViewType, Is.EqualTo(typeof(TestWindow)));
			Assert.That(desc.ControllerType, Is.EqualTo(typeof(TestWindowController)));

			Assert.That(desc.Completion, Is.Not.Null);
			Assert.That(desc.Completion.IsCompleted, Is.False);
			Assert.That(desc.Completion.IsCanceled, Is.False);
			Assert.That(desc.Completion.IsFaulted, Is.False);

			Assert.That(desc.IsCreated, Is.True);
			Assert.That(desc.Controller, Is.Not.Null);
			Assert.That(desc.Controller, Is.TypeOf<TestWindowController>());
			Assert.That(desc.Completion.IsCompleted, Is.False);
			Assert.That(desc.View, Is.Not.Null);
			Assert.That(desc.View, Is.TypeOf<TestWindow>());
			Assert.That(desc.Window, Is.Not.Null);
			Assert.That(desc.Window, Is.SameAs(desc.View));
			Assert.That(desc.Window.DataContext, Is.SameAs(viewModel));
			Assert.That(View.GetEnvironment(desc.Window), Is.SameAs(environment));
			Assert.That(View.GetOptions(desc.Window), Is.SameAs(options));

			desc.Dispose();

			Assert.That(desc.ViewType, Is.EqualTo(typeof(TestWindow)));
			Assert.That(desc.ControllerType, Is.EqualTo(typeof(TestWindowController)));

			Assert.That(desc.Completion, Is.Not.Null);
			Assert.That(desc.Completion.IsCompleted, Is.True);
			Assert.That(desc.Completion.IsCanceled, Is.False);
			Assert.That(desc.Completion.IsFaulted, Is.False);

			Assert.That(desc.IsCreated, Is.True);
			Assert.That(desc.Controller, Is.Not.Null);
			Assert.That(desc.Controller, Is.TypeOf<TestWindowController>());
			Assert.That(desc.Completion.IsCompleted, Is.True);
			Assert.That(desc.View, Is.Not.Null);
			Assert.That(desc.View, Is.TypeOf<TestWindow>());
			Assert.That(desc.Window, Is.Not.Null);
			Assert.That(desc.Window, Is.SameAs(desc.View));
			Assert.That(desc.Window.DataContext, Is.SameAs(viewModel));
			Assert.That(View.GetEnvironment(desc.Window), Is.SameAs(environment));
			Assert.That(View.GetOptions(desc.Window), Is.SameAs(options));
		}

		[Test, RequiresSTA]
		public void Test_Clone()
		{
			var environment = new AgentEnvironment();
			var options = new AgentOptions();
			var desc = new TestWindowDescription();

			desc.Create(environment, options);

			Assert.Throws<InvalidOperationException>(() =>
				{
					desc.Create(environment, options);
				});

			desc.Dispose();

			var desc2 = (TestWindowDescription)((ICloneable)desc).Clone();

			Assert.That(desc2.ViewType, Is.EqualTo(typeof(TestWindow)));
			Assert.That(desc2.ControllerType, Is.EqualTo(typeof(TestWindowController)));

			Assert.That(desc2.Completion, Is.Not.Null);
			Assert.That(desc2.Completion.IsCompleted, Is.False);
			Assert.That(desc2.Completion.IsCanceled, Is.False);
			Assert.That(desc2.Completion.IsFaulted, Is.False);

			Assert.That(desc2.IsCreated, Is.False);
			Assert.That(desc2.Controller, Is.Null);
			Assert.That(desc2.View, Is.Null);
			Assert.That(desc2.Window, Is.Null);

			Assert.DoesNotThrow(() =>
				{
					desc2.Create(environment, options);
				});

			desc2.Dispose();
		}

	}
}

#endif
